package com.aha.secondchapter.fifth;

import java.util.Arrays;

/**
 * 模拟链表
 * Created by wangxin on 2015/11/2.
 */
public class SimulationLinkedList {
    private int[] data = new int[101];
    private int[] right = new int[101];

    public static void main(String[] args) {
        SimulationLinkedList sl = new SimulationLinkedList();
        //读入数
        int[] numArr = {2,3,5,8,9,10,18,26,32};
        int len = numArr.length;
        int rightLen = len;
        System.arraycopy(numArr,0,sl.data,0,numArr.length);
        //初始化right
        for (int i = 0; i < numArr.length; i++) {
            if(i!=numArr.length-1){
                sl.right[i] = i+1;
            }else{
                sl.right[i] = 0;
            }
        }
        System.out.println(Arrays.toString(sl.data));
        System.out.println(Arrays.toString(sl.right));
        //在8前边加一个值6
        sl.data[len] = 10;//先把6放入data中
        len++;
        //调整right 因为从小到大排序 所以把新填入的值放在第一个大于它的数前边，开始遍历
        int i=0;
        while (true){
            if(sl.data[sl.right[i]]>10){
                sl.right[rightLen] = sl.right[i];
                sl.right[i] = len-1;
                rightLen++;
                break;
            }
            i=sl.right[i];
        }
        System.out.println("插入后。。。。");
        System.out.println(Arrays.toString(sl.data));
        System.out.println(Arrays.toString(sl.right));
        //遍历
        i=0;
        while(true){
            System.out.println(sl.data[i]);
            i = sl.right[i];
            if(sl.right[i]==0) break;
        }
    }
    public void add(){
       // data[data.length]
    }
}
